//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.滑动窗口.定长滑动窗口;

import java.util.HashMap;

public class demo_2953 {
    public demo_2953() {
    }

    public static int countCompleteSubstrings(String word, int k) {
        int n = word.length();
        char[] chars = word.toCharArray();
        int count = 0;

        for(int i = 1; i <= n / k; ++i) {
            if (k == 1 && i == 1) {
                count += n;
            } else {
                int len = k * i;
                int left = 0;
                HashMap<Character, Integer> map = new HashMap();

                for(int j = 0; j < n; ++j) {
                    char c = chars[j];
                    map.put(c, (Integer)map.getOrDefault(c, 0) + 1);
                    if ((Integer)map.get(c) == 0) {
                        map.remove(c);
                    }

                    if (j >= len - 1) {
                        if (map.size() == i) {
                            Boolean[] flag = new Boolean[]{true};
                            map.forEach((key, value) -> {
                                if (value != k) {
                                    flag[0] = false;
                                }

                            });
                            if (flag[0]) {
                                for(int z = left; z < j; ++z) {
                                    char o1 = chars[z];
                                    char o2 = chars[z + 1];
                                    if (Math.abs(o1 - o2) > 2) {
                                        flag[0] = false;
                                        break;
                                    }
                                }
                            }

                            if (flag[0]) {
                                ++count;
                            }
                        }

                        char out = chars[left];
                        map.put(out, (Integer)map.getOrDefault(out, 0) - 1);
                        if ((Integer)map.get(out) == 0) {
                            map.remove(out);
                        }

                        ++left;
                    }
                }
            }
        }

        return count;
    }

    public static void main(String[] args) {
        System.out.println(countCompleteSubstrings2("abb", 1));
    }

    public static int countCompleteSubstrings2(String word, int k) {
        int n = word.length();
        int ans = 0;

        int st;
        for(int i = 0; i < n; ans += f(word.substring(st, i), k)) {
            for(st = i++; i < n && Math.abs(word.charAt(i) - word.charAt(i - 1)) <= 2; ++i) {
            }
        }

        return ans;
    }

    private static int f(String S, int k) {
        char[] s = S.toCharArray();
        int res = 0;

        for(int m = 1; m <= 26 && k * m <= s.length; ++m) {
            int[] cnt = new int[26];

            for(int right = 0; right < s.length; ++right) {
                ++cnt[s[right] - 97];
                int left = right + 1 - k * m;
                if (left >= 0) {
                    boolean ok = true;

                    for(int i = 0; i < 26; ++i) {
                        if (cnt[i] > 0 && cnt[i] != k) {
                            ok = false;
                            break;
                        }
                    }

                    if (ok) {
                        ++res;
                    }

                    --cnt[s[left] - 97];
                }
            }
        }

        return res;
    }
}
